package com.muyu.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName: MpMetaObjectHandler
 * @Author: Zirun.Xin
 * @Date: 2025/4/9 15:51
 * @Version: 1.0
 * @Description: mybatis-plus填充处理器
 */
@Component
public class MpMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入时的填充策略
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createBy", "Zirun.Xin", metaObject);
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", "Zirun.Xin", metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    /**
     * 更新时的填充策略
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateBy", "Zirun.Xin", metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
